﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyDB;
using MyClassLibrary;

namespace MyProgram
{
    public partial class FrmAdd : Form
    {
        public FrmAdd()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.btn_AddPatientsOK.Enabled = false;
        }
        void VisualCue()
        {
            if (textBox3.Text.Trim() == "")  //必填欄位 姓名
            {
                btn_AddPatientsOK.Enabled = false;
            }
            else
            {
                btn_AddPatientsOK.Enabled = true; 
            }
        }

        bool m = true;
        FrmPatient p = new FrmPatient();
        private void btn_AddPatientsOK_Click(object sender, EventArgs e)
        {
            u = 0;
            if (textBox4.Text.Trim() == "")
            { MessageBox.Show("請輸入身分證字號"); }
            if (textBox4.Text.Trim().Length < 10)
            { MessageBox.Show("身分證字號 過短"); }
            else if (textBox4.Text.Trim().Length > 10)
            { MessageBox.Show("身分證字號 過長"); }
            else
            {
                //身分證字號
                String pId = textBox4.Text;
                using (SqlConnection Conn = new SqlConnection(Properties.Resources.MedicareConnectionString))
                {
                    string strCompare = "Select 身分證字號 from Patients";
                    using (SqlCommand cmd = new SqlCommand(strCompare, Conn))
                    {
                        cmd.CommandType = CommandType.Text;
                        Conn.Open();

                        SqlDataReader acdr = cmd.ExecuteReader();
                        while (acdr.Read())
                        {

                            String s = acdr["身分證字號"].ToString();
                            if (s == pId)
                            {
                                MessageBox.Show("身分證字號 重複");
                                m = false;
                                return;
                            }
                        }
                    }

                    if (m)    //write to DB ==============LinQ ===============
                    {
                        MedicareDataClassesDataContext db = new MedicareDataClassesDataContext();

                        var q = (from p in db.Patients
                                 select p.病患ID).Max();

                        db.Patients.InsertOnSubmit(new MyDB.Patients
                        {
                            姓名 = textBox3.Text,
                            身分證字號 = textBox4.Text,
                            出生年月日 = dateTimePicker1.Value,
                            性別 = comboBox1.SelectedText,
                            連絡電話 = textBox5.Text,
                            地址 = textBox6.Text,
                            Email = textBox7.Text,
                            VIP = checkBox1.Checked,
                            檢驗是否提醒 = checkBox2.Checked,
                            用藥是否提醒 = checkBox3.Checked,
                            //病歷號碼 = int.Parse(textBox2.Text)
                            病歷號碼 = q + 1
                        });
                        try
                        {
                            db.SubmitChanges();
                            MessageBox.Show("新增成功!");
                            this.Close();
                        }
                        catch (System.Data.SqlClient.SqlException ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
            MedicareDataClassesDataContext db1 = new MedicareDataClassesDataContext();
            ClsRecord.Record(sender, this.Name, db1.Patients.Max(n => n.病患ID));
        }

        private void FrmAdd_KeyUp(object sender, KeyEventArgs e)
        {
            VisualCue();
        }

        private void FrmAdd_TextChanged(object sender, EventArgs e)
        {
            VisualCue();
        }
        public static int u;
        private void button2_Click(object sender, EventArgs e)
        {
            u = 1;
            this.Close();
        }
    }
}
